#!/usr/bin/python
import subprocess
import re
import os
import utility

class Flowtool:
    
    def __init__(self):
        self.__setup__()
        self.u = utility.Utility()
        
    def __setup__(self):
        self.flowtool_path = '/home/dev/Desktop/flowtool/flowtool'
        self.config_path = '/home/dev/Desktop/flowtool/config.config'
        self.capreader_path = '/home/dev/workspace/3.0_Dev/tools/capreader/'
        self.output = '/home/dev/Desktop'
        
    def make_flowtool_cmd(self, flowtool_path, config_path, capreader_path, pcap_path):
        flowtool_path = self.u.remove_postfix_slash(flowtool_path)
        flowtool_cmd = str(flowtool_path)
        flowtool_cmd += ' -o ' + str(config_path)
        flowtool_cmd += ' -V'
        flowtool_cmd += ' -D ' + str(capreader_path)
        flowtool_cmd += ' ' + str(pcap_path)
        return flowtool_cmd
    
    def run_command(self, cmd):
        subprocess.call(cmd, shell=True)
        
    def run_flowtool(self, file_path):
        if os.path.isfile(file_path):
            cmd = self.make_flowtool_cmd(self.flowtool_path, self.config_path, self.capreader_path, file_path)
            self.run_command(cmd)
        else:
            print "Not a valid file %s" % (file_path)
    
    def run_flowtool_folder(self, directory):
        if os.path.isdir(directory):
            files = utility.Directory(directory, ".pcap")
            print "Total number of file: %s" % (str(files.count()))
            for f in files:
                print "Flowtool: " + f + "\t" +files.percentage(f)
                self.run_flowtool(f)
        else:
            print "Not a valid directory %s" % (directory)
            
        
